﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AutoChess
{
   class HitData
    {
        public ShipUnit hitter;
        public Vector3 hitPos;
        public HitData(ShipUnit hitter, Vector3 hitPos)
        {
            this.hitter = hitter;
            this.hitPos = hitPos;
        }
    }
     class ShipUnit : IVisualData<ShipUnit, ShipRenderer>
    {
        private int shipId;
        private Vector3 position;

        private PlayerEntity player;
        
        private ShipRenderer shipRenderer;
        public ShipRenderer ShipRenderer
        {
            get { return shipRenderer; }
        }
        public int ShipId
        {
            get { return shipId; }
            set { shipId = value;}
        }
        public int ShipModelId
        {
            get
            {
                var modelTable = Table.Blo.TableShipBlo.GetTableShip(shipId);
                if (modelTable == null)
                {
                    Debug.LogError("船ID："+shipId +"不存在！");
                    modelTable = Table.Blo.TableShipBlo.GetTableShip(Table.DataModel.TableConfig.autochessDefaultShip);
                }
                return modelTable.modelId;
            }
        }
        public Vector3 Position
        {
            get { return position; }
            set { position = value;}
        }
        public bool IsLeft 
        {
            get
            {
                return player.playerGuid == AutoChessManager.Ins.cacheData.CurPlayerData.GUID;
            }
        }
        public ShipUnit(PlayerEntity player)
        {
            this.player = player;
        }

        public PlayerEntity GetShipUnit()
        {
            return player;
        }

        public void FireTheHole(ShipUnit target,CommonAnimCurve cure)
        {
            if (shipRenderer != null)
            {
                shipRenderer.FireTarget(cure, target);
            }
        }

        /// <summary>
        /// 曲线寻路
        /// </summary>
        /// <param name="aniCurve"></param>
        /// <param name="onAutoPathEnd"></param>
        public void AutoPath(CommonAnimCurve aniCurve ,Action onAutoPathEnd)
        {
            if (shipRenderer != null)
            {
                shipRenderer.AutoPath(aniCurve, onAutoPathEnd);
            }
        }

        //受击
        public void OnHit(HitData hitData)
        {
            if (shipRenderer != null)
            {
                shipRenderer.OnHit(hitData);
            }
            Yunchang.EventManager.TriggerEvent(CommonEvent.ON_Refresh_Cache_HP);//
        }

        public void ConnectRenderer(ShipRenderer renderer)
        {
            if (renderer == null)
            {
                Debug.LogError("ShipUnit connect renderer failed.RD is null.");
                return;
            }
            if (shipRenderer != null)
                DisconnectRenderer();

            shipRenderer = renderer;
            shipRenderer.OnConnect(this);
        }

        public void DisconnectRenderer()
        {
            if (shipRenderer != null)
            {
                shipRenderer.OnDisconnect();
                shipRenderer = null;
            }
        }
    }
}
